package com.qk.management.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.dto.activity.ActivityDTO;
import com.qk.entity.Activity;
import com.qk.entity.Clue;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.service.ActivityService;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * &#064;author：zzq
 * &#064;createTime：2025/10/09 10:54
 * &#064;description： 
 */
@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ClueMapper clueMapper;
    // 提取数据校验逻辑为私有方法
    private void validateActivity(Activity activity) {
        if (activity == null) {
            throw new RuntimeException("活动信息不能为空");
        }
        if (activity.getChannel() == null) {
            throw new RuntimeException("活动渠道不能为空");
        }
        if (StringUtils.isBlank(activity.getName())) {
            throw new RuntimeException("活动名称不能为空");
        }
        if (activity.getStartTime() == null) {
            throw new RuntimeException("活动开始时间不能为空");
        }
        if (activity.getEndTime() == null) {
            throw new RuntimeException("活动结束时间不能为空");
        }
        if (StringUtils.isBlank(activity.getDescription())) {
            throw new RuntimeException("活动描述不能为空");
        }
        if (activity.getType() == null) {
            throw new RuntimeException("活动类型不能为空");
        }
    }
    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public PageResult<Activity> list(ActivityDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<Activity> list = activityMapper.selectByPage(dto);
        Page<Activity> page = (Page<Activity>) list;
        return PageResult.<Activity>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }

    @Override
    public void addActivity(Activity activity) {
        //数据校验channel,name,startTime,endTime,description,type,discount为必填
        validateActivity(activity);
        //数据补全
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.insert(activity);
    }

    @Override
    public void deleteById(Integer id) {
        // 业务校验，如果当前活动被线索引用则无法删除
        // 根据活动id查询线索
        List<Clue> clues = clueMapper.selectList(Wrappers.lambdaQuery(Clue.class).eq(Clue::getActivityId, id));
        // 如果有线索引用该活动则不能删除
        if (!clues.isEmpty()) {
            log.error("当前活动{}被线索 引用，无法删除", id);
            CommonException.throwCommonException(ParamEnum.ACTIVITY_REFERENCE_ERROR);
        }

        activityMapper.deleteById(id);
    }

    @Override
    public Activity get(Integer id) {
        return activityMapper.selectById(id);
    }

    @Override
    public void update(Activity activity) {
        //数据校验
        validateActivity(activity);
        //数据补全
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    @Override
    public List<Activity> listByType(String type) {
        return activityMapper.listByType(type);
    }
}
